<template>
  <!-- 添加或修改质检样品申请对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="20">
            <el-col :span="24">
              <el-form-item label="产品分类" prop="goodsType">
                <treeselect v-model="form.goodsType" :options="typeOptions" placeholder="请选择产品分类" @select="selectGoodsType"
                            />
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="产品选择" prop="goodsId">
                <el-select v-model="form.goodsId" placeholder="请选择产品" filterable ref="goodsId">
                  <el-option v-for="item in goodsList"
                             :key="item.id" :label="item.name+'('+item.model+')'" :value="item.id"/>
                </el-select>
              </el-form-item>
            </el-col>

            <el-col :span="24">
              <el-form-item label="产品数量" prop="count">
                <el-input-number v-model="form.count" placeholder="请输入产品数量"/>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="使用理由" prop="reason">
                <el-input type="textarea" v-model="form.reason" placeholder="请输入使用理由"/>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getAction,addAction, getById, updateAction} from '@/api/manage';
import moment from 'moment';
import {listByLazyTree} from "@/api/sysArea/area";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "GoodsSampleApplyForm",
  components: {Treeselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        goodsType: [
          {required: true, message: "请选择产品分类", trigger: "blur"}
        ],
        goodsId: [
          {required: true, message: "请选择产品", trigger: "blur"}
        ],
        count: [
          {required: true, message: "数量不能为空", trigger: "blur"},
          {validator:this.validator,trigger: 'blur'}
        ],
        reason: [
          {required: true, message: "理由不能为空", trigger: "blur"}
        ],
      },
      typeOptions: [],
      goodsList: [],
      url: {
        getById: '/goodsSampleApply/goodsSampleApply/',
        add: '/goodsSampleApply/goodsSampleApply/add',
        edit: '/goodsSampleApply/goodsSampleApply/edit',
        getGoodsList: '/goodsOrderIn/goodsOrderIn/list',
      },
    };
  },
  created() {
    this.getTreeselect();
  },
  methods: {
    validator(rule, value, callback) {
      if (value === 0) {
        callback(new Error('不能为0'));
      } else {
        callback();
      }
    },
    /** 查询类别 */
    getTreeselect() {
      listByLazyTree().then(response => {
        this.typeOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        goodsId: null,
        goodsCode: null,
        goodsName: null,
        goodsModel: null,
        count: null,
        reason: null,
        status: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加质检样品申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getById(this.url.getById, id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改质检样品申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateAction(this.url.edit, this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('ok');
            });
          } else {
            addAction(this.url.add, this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('ok');
            });
          }
        }
      });
    },


    selectGoodsType(e){
      this.goodsType = e.id;
      this.getGoodsList();
    },

    getGoodsList(){
      let params = {};
      params.deptId = this.goodsType;
      getAction(this.url.getGoodsList, params).then(response => {
        this.goodsList = response.rows;
      });
    }







  }
};
</script>
